# Contra
# Stage 2: Base 1 of Contra
# Haroon Khalid - 03/04/2009

import pygame
import sys

# INITIALIZE PYGAME
pygame.init()

# SET WINDOW CAPTION
pygame.display.set_caption("Contra: Stage 2 - Base 1")

# SET WINDOW RESOLUTION
resolution = (480, 320)
screen = pygame.display.set_mode(resolution, 0, 32)

# SET UP THE FONT, SIZE AND COLOR
font = pygame.font.Font("galette-med.otf", 24)
green = (255,255,0)

# SET UP LISTS
bullets = []
circles = []
players = []			

class Animation: 
    # data = [ [time, image], [time, image], ...] 
    def __init__(self, data): 
        self.data = data
        self.cur_frame = 0
        self.ticks = pygame.time.get_ticks()
        self.ticks_remaining = data[0][0]
        self.pos = [0, 0]
 
    def draw(self, dest):
        old_ticks = self.ticks
        self.ticks = pygame.time.get_ticks()
        tick_difference = self.ticks - old_ticks
        self.ticks_remaining -= tick_difference
 
        while (self.ticks_remaining <= 0):
            self.cur_frame += 1
            self.cur_frame %= len(self.data)
            self.ticks_remaining += self.data[self.cur_frame][0]
 
        dest.blit(self.data[self.cur_frame][1], self.pos)       
            
class Player(object):
    def __init__(self):
        self.image = p1_shoot
        self.rect = self.image.get_rect()
        self.rect.x = 220
        self.rect.y = 210
        self.health = 100
        self.lives = 5 
        self.shooting = Animation([[100, p1_shoot]])
        self.moving_right = Animation([[150, p1_walk_1],[150, p1_walk_2],[150, p1_walk_3],[150, p1_walk_4],[150, p1_walk_5],[150, p1_walk_3]])
        self.anim = self.shooting
 
    def draw(self, dest):
        self.anim.pos = self.rect
        self.anim.draw(dest)
 
    def update(self):         
        if self.health < 1:
            self.anim = self.moving_right
 
        self.draw(screen) 

# CIRCLE FOR PASSING STAGE        
class Circle(object):
    def __init__(self):
        self.image = circle_pass_3
        self.rect = self.image.get_rect()
        self.rect.x = 240
        self.rect.y = 200
        self.health = 5   
        self.living = Animation([[100, circle_pass_1],[100, circle_pass_2], [100, circle_pass_3]])
        self.anim = self.living
        
    def draw(self, dest):
        self.anim.pos = self.rect
        self.anim.draw(dest)
        
    def update(self):
        if self.health < 1:
            circles.remove(self)
            enemyhit.play()
            # draw explosion
        else:
            self.draw(screen)

# BULLET FOR PLAYER            
class Bullet(object):
    def __init__(self):
        self.image = bullet
        self.rect = self.image.get_rect()
        self.rect.x = 0
        self.rect.y = 0
        self.p1x = 0
        
    def update(self):
        if self.rect.y < 150:
            bullets.remove(self) 
        else:
            self.rect.y -=10
        screen.blit(self.image, (self.rect.x, self.rect.y))

# CREATE BULLET
def create_bullet(): 
    b = Bullet()
    b.rect.x = p1.rect.x + 15
    b.rect.y = p1.rect.y
    bullets.append(b)

# UPDATE ALL BULLETS IN BULLETS LIST    
def update_bullets():     
    for b in bullets:
        b.update()

'''        
# CREATE PLAYER
def create_player():
    p = Player()
    players.append(p)

# UPDATE PLAYER
def update_player():
    for p in players:
        p.update()
'''

# CREATE CIRCLE        
def create_circle(x,y):
    c = Circle()
    c.rect.x = x
    c.rect.y = y
    circles.append(c)

# UPDATE ALL CIRCLES IN CIRCLES LIST    
def update_circles():
    for c in circles:
        c.update()    
    
       
# LOAD GRAPHICS
bg = 'contra-background.png'
background = pygame.image.load(bg).convert()
p1walk1 = 'p1_walk_1.png'
p1_walk_1 = pygame.image.load(p1walk1).convert()
p1_walk_1.set_colorkey((255,0,255))
p1walk2 = 'p1_walk_2.png'
p1_walk_2 = pygame.image.load(p1walk2).convert()
p1_walk_2.set_colorkey((255,0,255))
p1walk3 = 'p1_walk_3.png'
p1_walk_3 = pygame.image.load(p1walk3).convert()
p1_walk_3.set_colorkey((255,0,255))
p1walk4 = 'p1_walk_4.png'
p1_walk_4 = pygame.image.load(p1walk4).convert()
p1_walk_4.set_colorkey((255,0,255))
p1walk5 = 'p1_walk_5.png'
p1_walk_5 = pygame.image.load(p1walk5).convert()
p1_walk_5.set_colorkey((255,0,255))
p1shoot = 'p1shoot.png'
p1_shoot = pygame.image.load(p1shoot).convert()
p1_shoot.set_colorkey((255,0,255))
p1shootdown = 'p1shootdown.png'
p1_shootdown = pygame.image.load(p1shootdown).convert()
p1_shootdown.set_colorkey((255,0,255))
whitebullet = 'bullet.png'
bullet = pygame.image.load(whitebullet).convert()
bullet.set_colorkey((0,0,0))
machineshoot = 'machineshoot.png'
machine_shoot = pygame.image.load(machineshoot).convert()
machine_shoot.set_colorkey((255,0,255))
circlepass1= 'circlepass1.png'
circle_pass_1 = pygame.image.load(circlepass1).convert()
circle_pass_1.set_colorkey((255,0,255))
circlepass2= 'circlepass2.png'
circle_pass_2 = pygame.image.load(circlepass2).convert()
circle_pass_2.set_colorkey((255,0,255))
circlepass3= 'circlepass3.png'
circle_pass_3 = pygame.image.load(circlepass3).convert()
circle_pass_3.set_colorkey((255,0,255))

# LOAD SOUNDS
music = pygame.mixer.Sound('music.ogg')
music.set_volume(0.4)
machinehit = pygame.mixer.Sound('machinehit.ogg')
bulletshoot = pygame.mixer.Sound('bulletshoot.ogg')
enemyhit = pygame.mixer.Sound('enemyhit.ogg')
special = pygame.mixer.Sound('special.ogg')

p1 = Player()

# GET USER INPUT
def test_input():
    for event in pygame.event.get():
        if event.type == pygame.QUIT or event.type == pygame.K_ESCAPE:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                p1.rect.x += 10                
            if event.key == pygame.K_LEFT:
                p1.rect.x -= 10
            if event.key == pygame.K_DOWN:
                p1.image = p1_shootdown
            if event.key == pygame.K_SPACE:
                bulletshoot.play()
                create_bullet()
                
# DISPLAY STATS ON SCREEN
def draw_stats():
    p1_coord = font.render("Player 1: " + str(p1.rect.x) + "," + str(p1.rect.y), True, green)
    screen.blit(p1_coord, (5, 5)) 

# COLLISION DETECTION - CIRCLES/BULLETS
def check_circle_hit():
    for b in bullets:
        for c in circles:
            if b.rect.colliderect(c.rect):
                machinehit.play()
                print "Circle hit: " + str(b)
                c.health -= 1
                if b in bullets:
                    bullets.remove(b)

# LEVEL 1
def level_1():
    create_circle(224,125)
    all_dead = 0
    while all_dead == 0:      
        screen.blit(background, (0,0))
        draw_stats()
        test_input()
        p1.update()
        update_bullets()
        update_circles()
        check_circle_hit()
        pygame.display.update()
        pygame.time.delay(25)
        all_dead = 1
        for c in circles:
            if c.health != 0:
                all_dead = 0

# LEVEL 2
def level_2():
    level_2_exit = 0
    create_circle(180,140)
    create_circle(280,140)
    all_dead = 0
    while all_dead == 0:      
        screen.blit(background, (0,0))
        draw_stats()
        test_input()
        p1.update()
        update_bullets()
        update_circles()
        check_circle_hit()
        pygame.display.update()
        pygame.time.delay(25)
        all_dead = 1
        for c in circles:
            if c.health != 0:
                all_dead = 0
                
# LEVEL 3
def level_3():
    create_circle(175,120)
    create_circle(273,120)
    create_circle(175,80)
    create_circle(273,80)
    all_dead = 0
    while all_dead == 0:      
        screen.blit(background, (0,0))
        draw_stats()
        test_input()
        p1.update()
        update_bullets()
        update_circles()
        check_circle_hit()
        pygame.display.update()
        pygame.time.delay(25)
        all_dead = 1
        for c in circles:
            if c.health != 0:
                all_dead = 0
                        

# MUSIC SETTINGS
musicloop = 1

# MAIN GAME LOOP
while True:
    if musicloop == 1:
        music.play()
        musicloop = 0
        level_1()
        level_2()
        level_3()